package eu.szemraj.utils {
   
   import flash.text.TextLineMetrics;   
   import flash.geom.Rectangle;   
   import flash.filters.ColorMatrixFilter;   
   import flash.text.TextField;   

   public class TextFieldUtil {

      private static const byteToPerc:Number = 1 / 0xff;

      public static function setTextFieldSelectionColors( textField:TextField, textColor:uint, selectionColor:uint, selectedTextColor:uint ):void {
			
			
         var cmf:ColorMatrixFilter = new ColorMatrixFilter( );
         textField.textColor = 0xFF0000;
         
         var o:Array = [selectionColor >> 16 & 0xff, selectionColor >> 8 & 0xff, selectionColor & 0xff];         var r:Array = [textColor >> 16 & 0xff, textColor >> 8 & 0xff, textColor & 0xff];         var g:Array = [selectedTextColor >> 16 & 0xff, selectedTextColor >> 8 & 0xff, selectedTextColor & 0xff];
         
         var ro:int = o[0];
         var go:int = o[1];
         var bo:int = o[2];
			
         var rr:Number = ((r[0] - 0xff) - o[0]) * byteToPerc + 1;
         var rg:Number = ((r[1] - 0xff) - o[1]) * byteToPerc + 1;
         var rb:Number = ((r[2] - 0xff) - o[2]) * byteToPerc + 1;

         var gr:Number = ((g[0] - 0xff) - o[0]) * byteToPerc + 1 - rr;
         var gg:Number = ((g[1] - 0xff) - o[1]) * byteToPerc + 1 - rg;
         var gb:Number = ((g[2] - 0xff) - o[2]) * byteToPerc + 1 - rb;
			
         cmf.matrix = [rr, gr, 0, 0, ro, rg, gg, 0, 0, go, rb, gb, 0, 0, bo, 0, 0, 0, 1, 0];
			
         textField.filters = [cmf];
      }

      public static function getTextFieldBounds( textField:TextField ):Rectangle {
      	
         var numLines:int = textField.numLines;
      	
         var w:Number = 0;
         var h:Number = 0;
      	
         for( var i:int = 0; i < numLines ; ++i ) {
      		
            var lineMetrics:TextLineMetrics = textField.getLineMetrics( i );
            w = Math.max( w, lineMetrics.width );
            h += lineMetrics.height;
         }
         w += 4;
         h += 4;
         return new Rectangle( textField.x, textField.y, w, h );
      }
   }
}
